package com.example.basecore.context;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author ycd0075
 * @date 2024-01-25 23:11
 */
public class SpringContext implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContext.applicationContext = applicationContext;
    }

    public static <T> T getBean(String name, Class<T> requiredType) throws BeansException {
        T bean = null;
        try {
            bean = applicationContext.getBean(requiredType);
        } catch (BeansException e){
            throw e;
        }
        return bean;
    }
}
